package com.dudu.controller;

import com.dudu.dto.ResultDto;
import com.dudu.service.WeiPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/order/weixin")
public class WeiPayController {

    @Autowired
    private WeiPayService weiPayService;

    @GetMapping("/getPayStatus/{orderId}")
    public ResultDto getPayStatus(@PathVariable Long orderId) {
        System.out.println(orderId+"-----11");
        Map<String, String> map = weiPayService.queryPayStatus(orderId);
        if (map == null) {
            return ResultDto.error("查询失败", null);
        }
        //查询成功+支付成功
        if ("SUCCESS".equals(map.get("trade_state"))) { //支付成功
            weiPayService.paySuccess(orderId, map);//更新了订单状态0 1 +支付记录表的支付状态:1 2
            return ResultDto.success("支付成功", null);
        }
        //
        return ResultDto.success("支付中",null); //包含支付失败的情况
    }

    @GetMapping("/{orderId}")
    public ResultDto pay(@PathVariable Long orderId) {
        String url = weiPayService.pay(orderId);
        return ResultDto.success("url", url);

    }
}
